package com.automation.service;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfDouble;
import org.opencv.core.CvType;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.io.File;

public class ImageUtils {
    static {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }

    public static boolean isHighQuality(File imageFile) {
        Mat image = Imgcodecs.imread(imageFile.getAbsolutePath());
        if (image.empty()) return false;

        // 检查分辨率
        if (image.width() < 600 || image.height() < 400) return false;

        // 使用拉普拉斯算子检测清晰度
        Mat gray = new Mat();
        Imgproc.cvtColor(image, gray, Imgproc.COLOR_BGR2GRAY);

        Mat laplacian = new Mat();
        Imgproc.Laplacian(gray, laplacian, CvType.CV_64F);

        MatOfDouble stddev = new MatOfDouble();
        Core.meanStdDev(laplacian, new MatOfDouble(), stddev);

        // 方差 > 100 视为清晰
        return Math.pow(stddev.get(0,0)[0], 2) > 100;
    }
}